setwd("C:/Users/Chad/Dropbox/Border Diffusion (C&O)/JOP RR Data Work/JOPRR2")
library(readstata13)
library(mgcv)
settledata<-read.dta13("GAMdata.dta")
gam.1<-gam(settlem~lag_row_wsettlem1 + lag_anytp + sumneg + maxicowsal0 + cow_civilwarcombine 
           + jtdem6 + lncincratio + atopally + crt07riv + dyad_ldrduration
           + totalrivals_nondyad + postww1 + postww2 + s(nosettleyr, bs="cr") 
           + s(lag_noothersetyr, bs="cr"), data=settledata, family=binomial)
summary(gam.1)
gam.results<-summary(gam.1)
gam.results$p.table
write.csv(gam.results$p.table, file="GAMresults1.csv")
gam.results$s.table
write.csv(gam.results$s.table, file="GAMresults2.csv")
predict1<-read.dta13("GAMPredict1.dta")
predict2<-read.dta13("GAMPredict2.dta")
predset13<-predict.gam(gam.1, newdata=predict1, se.fit=TRUE)
summary(predset13)
write.csv(predset13, file="GAMpredictions1.csv")
predset0<-predict.gam(gam.1, newdata=predict2, se.fit=TRUE)
write.csv(predset0, file="GAMpredictions0.csv")
